<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller {
	public $gm;
	public function __construct()
	{
		parent::__construct();
        $this->gm = D("Goods");
	}
    public function goodsadd(){
    	if(IS_POST){
    		if(!$this->gm->create($_POST)){
	    		$msg= $this->gm->getError();
	    		$this->error($msg,U("admin/Goods/goodsadd"),2);
    		 }else{
             $upload = new \Think\Upload();// 实例化上传类 
             $upload->maxSize = 3145728 ;// 设置附件上传大小
             $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
             // 设置附件上传类型 
             $upload->rootPath = './Public/images/';
             // 设置附件上传根目录 
             $upload->savePath = ''; // 设置附件上传（子）目录
            // 上传文件 
             $info = $upload->upload(); 
             if(!$info) {
            // 上传错误提示错误信息 
             $this->error($upload->getError());
             }else{
             // 上传成功 
               $imgpath="./Public/images/goods_img/".$info['goods_img']['savepath'].$info['goods_img']['savename'];
               $image = new \Think\Image();
               $image->open($imgpath);
               // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
               $thumb_imgpath="./Public/images/goods_thumbimg/thumb_".$info['goods_img']['savename'];
               $image->thumb(150, 150)->save($thumb_imgpath);
               $this->gm->goods_thumb=$thumb_imgpath;
               $this->gm->goods_img=$imgpath;
            }
               if($this->gm->add())
	    		 {
	    		 	$this->redirect("admin/goods/goodslist");
	    		 }
    		 }
    	}
        $this->display();
    }
    public function goodslist(){
        // $User = M('User'); 实例化User对象// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $p = I("p")?I("p"):1;
        $list = $this->gm->order('goods_id')->page($p.',5')->select();
        $this->assign('list',$list);
        // 赋值数据集
        $count = $this->gm->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出$this->display(); // 输出模板
        $this->display();
    }
    public function del()
    {
    	if($this->gm->delete(I("goods_id")))
    	{
           $this->success("删除成功",U("goods/goodslist"),2);
    	}else{
    	   $this->error("删除失败",U("goods/goodslist"),1);
    	}
    }
    public function goodsedit()
    {
    	if(IS_POST){
    		$data = I("post.");
    		$goodsId=$data['goods_id'];
    		unset($data['goods_id']);
    	  if($this->gm->where("goods_id=".$goodsId)->save($data)){
    	  	$this->success("修改成功！",U("admin/goods/goodslist"),1);
    	  	exit;
    	  }
    	}
        $this->assign("gdArr",$this->gm->find(I("goods_id")));
    	$this->display();
    }
}